<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="page_right combopage_right">
    <p class="combo_manage_batch">
        <a class="combo_manage_batchmanagebtn" href="javascript:;">批量编辑</a>
        <a class="combo_manage_batchdeletebtn" href="javascript:;">批量删除</a>
    </p>
    <p class="combo_managecheckall">全选&nbsp;&nbsp;<input type="checkbox"></p>
    <ul class="combo_managelist">
        <c:forEach items="${comboList}" var="data" varStatus="status">
        <li>
            <p>
                <label for="${data.discountid}" style="display:block;">
                    <i>${status.count}</i>
                    <span>${data.money} 元<span>${data.coin}</span> 币</span>
                    <input type="checkbox" id="${data.discountid}">
                </label>
            </p>
            <div class="combo_edit">
                <span>套餐币数：</span>
                <input type="number" value="${data.coin}" name="${data.discountid}">
            </div>
        </li>
        </c:forEach>
    </ul>
    <div><a class="combo_managebtn" href="javascript:;">保存</a></div>

</div>
<!-- 删除套餐 提示弹窗 -->
<div class="pointpage_dv">
    <div class="pointpage">
        <p>将永久删除这<span>1</span>个套餐</p>
        <p><a class="combo_pointpage_btn1" href="javascript:;">确认</a>  <a class="combo_pointpage_btn2" href="javascript:;">取消</a></p>
    </div>
</div>
<!-- 删除套餐 提示弹窗 end -->
<script type="application/javascript" >
    //批量编辑
    $(".combo_managebtn").hide();
    var combo_managevalue=new Array();
    function batchmanage(){
        $(".combo_manage_batchmanagebtn").on("click", function(){     //批量编辑按钮
            if($(".combo_manage_batchmanagebtn").text()=="批量编辑"){
                $(".combo_edit").css({"transition":"all 0.7s","left":"80px","opacity":"1","z-index":"1"});
                $(".combo_managebtn").stop().show().text("保存").css("background","#38adff");
                $(".combo_manage_batchmanagebtn").text("退出批量编辑");
                $(".combo_manage_batchdeletebtn").unbind("click");
            }else{
                $(".combo_edit").css({"transition":"all 0.3s","left":"500px","opacity":"0","z-index":"-1"});
                $(".combo_managebtn").stop().hide();
                $(".combo_manage_batchmanagebtn").text("批量编辑");
                batchdelete();
            }
        })
    };
    batchmanage();

    //批量删除
    $(".combo_managelist li p input, .combo_managecheckall").hide();
    function batchdelete(){
        $(".combo_manage_batchdeletebtn").on("click", function(){     //批量删除按钮
            if($(".combo_manage_batchdeletebtn").text()=="批量删除"){
                $(".combo_managelist li p input, .combo_managecheckall").stop().show();
                $(".combo_managebtn").stop().show().text("删除").css("background","#f00");
                $(".combo_manage_batchdeletebtn").text("退出批量删除");
                $(".combo_manage_batchmanagebtn").unbind("click");
            }else{
                $(".combo_managelist li p input, .combo_managecheckall").stop().hide();
                $(".combo_managebtn").stop().hide();
                $(".combo_manage_batchdeletebtn").text("批量删除");
                batchmanage();
            }
        });
    };
    batchdelete();

    //全选按钮
    $(".pointpage_dv").hide();
    $(".combo_managecheckall input").on("click", function(){     //批量删除按钮
        if($(".combo_managecheckall input").is(":checked")){
            $(".combo_managelist li p input").each(function(combo_index1){
                $(".combo_managelist li p input").eq(combo_index1)[0].checked=true;
            })
        }else{
            $(".combo_managelist li p input").each(function(combo_index2){
                $(".combo_managelist li p input").eq(combo_index2)[0].checked=false;
            })
        }
    });

    //分项子按钮
    $(".combo_managelist li p input").each(function(){
        $(this).click(function(){
            var combo_index3=0;
            if(!$(this).is(":checked")){
                $(".combo_managecheckall input")[0].checked=false;
            }else if($(this).is(":checked")){
                for(var combo_index4=0; combo_index4<$(".combo_managelist li").length; combo_index4++){
                    if($(".combo_managelist li p input").eq(combo_index4).is(":checked")){
                        combo_index3++;
                        if(combo_index3==$(".combo_managelist li").length){
                            $(".combo_managecheckall input")[0].checked=true;
                        }
                    }
                }
            }
        })
    });

    //保存or删除按钮
    $(".combo_managebtn").on("click", function(){
        if($(".combo_managebtn").text()=="保存"){
            var j=0;
            for(var i=0; i<$(".combo_managelist li").length; i++){
                combo_managevalue[i]=parseInt($(".combo_edit input:eq("+i+")").val());
                if(combo_managevalue[i]>999){
                    combo_managevalue[i]=999;
                }
                $(".combo_edit input:eq("+i+")").val(combo_managevalue[i]);
                if(combo_managevalue[i]>0){
                    var tempCombo=new Array();
                    var tempComboCoin = new Array();
                    $(".combo_edit input:eq("+i+")").css("box-shadow","none");
                    $(".combo_managelist li:eq("+i+") p span span").text(combo_managevalue[i]);
                    j++;
                    if(j==$(".combo_managelist li").length){
                        $(".combo_edit").css({"transition":"all 0.3s","left":"500px","opacity":"0","z-index":"-1"});
                        $(".combo_manage_batchmanagebtn").text("批量编辑");

                        for(var a=0; a<$(".combo_managelist li").length; a++){
                            tempCombo[a]=$(".combo_managelist li .combo_edit").eq(a).children().next("input").attr("name");
                            tempComboCoin[a]=$(".combo_managelist li .combo_edit").eq(a).children().next("input").val();
                        }
                        batchdelete();
                        editBatch(tempCombo,tempComboCoin);
                    }
                }else{
                    $(".combo_edit input:eq("+i+")").css("box-shadow","0 0 3px 1px #f00");
                }
            }
        }else if($(".combo_managebtn").text()=="删除"){
            var tempCombo=new Array();
            var index=0;
            var combo_deleteIndex=0;
            for(var combo_index5=0; combo_index5<$(".combo_managelist li").length; combo_index5++){
                if($(".combo_managelist li p input").eq(combo_index5).is(":checked")){
                    combo_deleteIndex++;
                    tempCombo[index]=$(".combo_managelist li p input").eq(combo_index5).attr("id")
                    index++;
                }
            }
            $(".pointpage_dv").stop().show();
            $(".pointpage p span").text(combo_deleteIndex);
            //删除确定按钮
            $(".combo_pointpage_btn1").on("click", function(){
                $(".pointpage_dv, .combo_managebtn, .combo_managelist li p input, .combo_managecheckall").stop().hide();
                $(".combo_managelist li p").find(":checked").parents("li").remove();
                $(".combo_manage_batchdeletebtn").text("批量删除");
                batchmanage();
                editBatchDel(tempCombo);
            })
            //删除取消按钮
            $(".combo_pointpage_btn2").on("click", function(){
                $(".pointpage_dv").stop().hide();
            })
        }
    });


    function editBatchDel(n){
        var discounts = eval(n);
        discounts = discounts.toString();
        $.ajax({
            url:'${basePath}v2/combo/editBatchDel',
            type : 'POST',
            data : {"discounts":discounts},
            success: function (ret) {
                if(ret.result==false) {
                    alert("默认套餐不能删除");
                }
            }
        });
    }

    function editBatch(n,v){
        var discounts = eval(n);
        var coins = eval(v);
        discounts = discounts.toString();
        coins = coins.toString();
        $.ajax({
            url:'${basePath}v2/combo/editBatch',
            type : 'POST',
            data : {"discounts":discounts,"coins":coins},
            success: function (ret) {
                if(ret.result==false) {
                    alert(ret.info);
                }
            }
        });
    }

</script>
